<?php

require_once 'classes/LightPressPlugin.php';

class HighlightPost extends LightPressPlugin {
    
    var $constructor_args = array(
        'highlight_categories'=>'category ids of the categories from which to get the latest post, separated by spaces',
        'substitutions'       =>'map a category id to a descriptive title to use in the template, in the format id:"title" id:"title"',
        'backgrounds'       =>'map a category id to a background to use in the template, in the format id:color id:color'
        );
    var $default_context = 255;
    var $description = 'Sets the last post\'s title and abstract from the given category in a box on the sidebar.';
    
    var $active = true;
    var $hooks = array('sidebar');
    
    var $highlight_categories = '';
    var $substitutions = '';
    var $backgrounds = '';
    
    function HighlightPost(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$payload) {
        $tpl =& $this->_frontend->tpl;
        $db =& $this->_frontend->db;
        $options =& $this->_frontend->options;
        $tables =& $this->_frontend->tables;
        
        $posts = array();
        
        foreach (explode(' ', $this->highlight_categories) as $category_id) {
            
            $category_id = trim($category_id);
            
            if (empty($category_id))
                continue;
        
            $q = "SELECT id, post_name as name, post_title as title, p.post_category, " .
                "UNIX_TIMESTAMP(p.post_date_gmt) + '" . $options['gmt_offset'] . "' as tstamp, " .
                "UNIX_TIMESTAMP(p.post_date_gmt) as tstamp_gmt " .
                "from " . $tables['posts'] . " p " .
                "INNER JOIN " . $tables['post2cat'] . " p2c on p.id=p2c.post_id " .
                "WHERE p.post_date_gmt <= '" . $this->_frontend->where_date . "' " .
                "AND (p.post_status='publish') AND p2c.category_id=$category_id " .
                "order by p.post_date desc limit 1";
            
            if ($db->query($q) == 0)
                continue;
            $post = $db->next();
            $post['category_id'] = $category_id;
            $posts[] = $post;
        }
        
        if (count($posts) == 0)
            return $this->hide();
        
        $category_titles = array();
        $matches = array();
        preg_match_all('/([0-9]+):"([^"]+)"\s*/', $this->substitutions, $matches, PREG_SET_ORDER);
        foreach ($matches as $m)
            $category_titles[$m[1]] = $m[2];
        
        $backgrounds = array();
        $matches = array();
        preg_match_all('/([0-9]+):([^\s]+)\s*/', $this->backgrounds, $matches, PREG_SET_ORDER);
        foreach ($matches as $m)
            $backgrounds[$m[1]] = $m[2];
        
        $tpl->setFile('plugin_highlightpost', 'plugins/highlight_post.xml');
        
        foreach ($posts as $p=>$post) {
            $posts[$p]['category'] = (isset($category_titles[$post['category_id']]) ? $category_titles[$post['category_id']] : '');
            $posts[$p]['tstamp'] = (int)$post['tstamp'];
            $posts[$p]['permalink'] = $this->_frontend->getPermalink($post['id'], $post['name'], $post['tstamp'], $post['post_category']);
            $posts[$p]['short_date'] = strftime($options['short_date_format'], $post['tstamp']);
            $posts[$p]['title'] = stripslashes($post['title']);
            $posts[$p]['background'] = (isset($backgrounds[$post['category_id']]) ? $backgrounds[$post['category_id']] : 'ffffff');
        }
        
        $tpl->parseBlock('PLUGIN_HIGHLIGHTPOSTPOST', 'plugin_highlightpost_post', $posts, 'plugin_highlightpost');
        $tpl->parse('PLUGIN_HIGHLIGHTPOST', 'plugin_highlightpost');    
    }
}

?>